package com.icss.service.impl;

import java.util.List;

import com.icss.dao.StudentDao;
import com.icss.model.Student;
import com.icss.model.StudentPage;
import com.icss.service.StudentService;

public class StudentServiceImpl implements StudentService{

	@Override
	public List<Student> findAll() {
		String sql = "select id,name,sex,tid from stu";
		return StudentDao.query(sql);
	}

	@Override
	public List<Student> findByPageName(int current, String name) {
		String sql = "select id,name,sex,tid from stu where name like ? limit ?,?";
		return StudentDao.query(sql, "%"+name+"%",(current-1)*StudentPage.PAGE_COUNT,StudentPage.PAGE_COUNT);
	}

	@Override
	public Student findById(int id) {
		String sql = "select id,name,sex,tid from stu where id = ?";
		if(StudentDao.query(sql, id).size() > 0) {
			return StudentDao.query(sql, id).get(0);
		}else {
			return null;
		}		
	}

	@Override
	public boolean update(Student s) {
		String sql = "update stu set name=?,sex=?,tid=? where id=?";
		return StudentDao.modify(sql, s.getName(),s.getSex(),s.getTid(),s.getId());
	}

	@Override
	public boolean add(Student s) {
		String sql = "insert into stu values(default,?,?,?)";
		return StudentDao.modify(sql, s.getName(),s.getSex(),s.getTid());
	}

	@Override
	public boolean del(int id) {
		String sql = "delete from stu where id = ?";
		return StudentDao.modify(sql, id);
	}

	@Override
	public List<Student> findByName(String name) {
		String sql = "select id,name,sex,tid from stu where name like ?";
		return StudentDao.query(sql, "%"+name+"%");
	}

}
